import base from './base/base64.js'
import md5 from './base/md5.js'
const httpIp = "http://114.115.159.244:8188";
const Request = {
	login(api_name = "", api_version = "", api_params = {}) {
		const inputparams = getAjaxParams(api_name, api_version, api_params)
		const traceid = (Math.random().toString(16).substr(2) + '-' +
			Math.random().toString(16).substr(2) + '-' +
			Math.random().toString(16).substr(2) + '-' +
			Math.random().toString(16).substr(2)).toUpperCase();
		uni.setStorageSync('traceid', traceid);
		let url = `${httpIp}` + "/api" + "?xkcuitraceid=" + traceid;
		return new Promise((resolve, reject) => {
			uni.request({
				url,
				data: inputparams,
				method: "POST",
				header: {
					'Accept': 'application/json',
					'Content-Type': 'application/json'
				},
				success: (response) => {
					if (response.data.flag === "003") {
						reject(response.data.kdjson)
					} else {
						resolve(response.data.kdjson)
					}
				},
				fail: (response) => {
					reject(response)
				}
			})
		})
	},
	post(api_name = "", api_version = "", api_params = {}) {
		const traceid = uni.getStorageSync('traceid');
		const url = `${httpIp}/` + "api/" + "?xkcuitraceid=" + traceid;
		const inputparams = getAjaxParams(api_name, api_version, api_params)
		return new Promise((resolve, reject) => {
			uni.request({
				url,
				data: inputparams,
				method: "POST",
				header: {
					'Accept': 'application/json',
					'Content-Type': 'application/json'
				},
				success: (response) => {
					if (response.data.kdjson.flag === "003") {
						reject(response.data.kdjson)
					} else if (response.data.kdjson.flag === "001") {
						let path = getCurrentPages()[0].route;
						uni.reLaunch({
							url: "/pages/login/login?callBackUrl=" + path,
						})
						reject(response.data.kdjson);
					} else {
						resolve(response.data.kdjson)
					}
				},
				fail: (response) => {
					reject(response)
				}
			})
		})

	}
}
export default Request;
const getAjaxParams = (a, v, p) => {
	var random = Math.floor(Math.random() * 10) % 3;
	if (random == 0) {
		return encryption.get16(a, v, p);
	} else if (random == 1) {
		return encryption.getK(a, v, p);
	} else {
		return encryption.getL(a, v, p);
	}
}
//encryption加密参数
const encryption = {
	get16: function(a, v, p) {
			var pp = {};
			var _t = new Date().getTime() + "";
			var _p = JSON.stringify(p);
			pp._0x0111 = base.base64.encoder(_t);
			pp._0x1011 = base.base64.encoder(a);
			pp._0x1100 = base.base64.encoder(v);
			pp._0x1110 = base.base64.encoder(encodeURIComponent(_p));
			pp._0x1001 = md5.md5(pp._0x0111 + pp._0x1011 + pp._0x1100 + pp._0x1110).toUpperCase()
			pp._0x1101 = base.base64.encoder("localhost");
			return pp;
		} //get16
		,
	getK: function(a, v, p) { //_params.._version .. _timestamp .. _api_name
			var pp = {};
			var _t = new Date().getTime() + "";
			var _p = JSON.stringify(p);
			pp.KInGDOM = base.base64.encoder(_t);
			pp.KINGdOM = base.base64.encoder(a);
			pp.KINGDoM = base.base64.encoder(v);
			pp.KiNGDOM = base.base64.encoder(encodeURIComponent(_p));
			pp.kINGDOM = md5.md5(pp.KiNGDOM + pp.KINGDoM + pp.KInGDOM + pp.KINGdOM).toUpperCase()
			pp.KINgDOM = base.base64.encoder("localhost");
			pp.KINGDOm = base.base64.encoder("http");
			return pp;
		} //getK
		,
	getL: function(a, v, p) {
		var pp = {};
		var _t = new Date().getTime() + "";
		var _p = JSON.stringify(p);
		pp.css = base.base64.encoder(_t);
		pp.android = base.base64.encoder(a);
		pp.html = base.base64.encoder(v);
		pp.ios = base.base64.encoder(encodeURIComponent(_p));
		pp.js = md5.md5(pp.ios + pp.android + pp.css + pp.html).toUpperCase()
		pp.wp = base.base64.encoder("localhost");
		return pp;
	} //getL
}
